package com.usian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.*;
import com.usian.pojo.*;
import com.usian.utils.IDUtils;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemService {
@Autowired
private TbItemMapper tbItemMapper;
@Autowired
private TbItemCatMapper tbItemCatMapper;
@Autowired
private TbItemParamMapper tbItemParamMapper;
@Autowired
private TbItemDescMapper tbItemDescMapper;
@Autowired
private TbItemParamItemMapper tbItemParamItemMapper;


    public TbItem getId(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }

    public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
        PageHelper.startPage(page,rows);
        //查询状态是1 并且  按修改时间逆序排列
        TbItemExample tbItemExample = new TbItemExample();
        tbItemExample.setOrderByClause("updated DESC");
        TbItemExample.Criteria criteria = tbItemExample.createCriteria();
        criteria.andStatusEqualTo((byte)1);
        List<TbItem> tbItemList = tbItemMapper.selectByExample(tbItemExample);
        PageInfo<TbItem> tbItemPageInfo = new PageInfo<>(tbItemList);
        return new PageResult(page,tbItemPageInfo.getTotal(),tbItemPageInfo.getList());
    }

    public List<TbItemCat> selectItemCategoryByParentId(Long id) {
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria = tbItemCatExample.createCriteria();
        criteria.andParentIdEqualTo(id);
        criteria.andStatusEqualTo(1);
       List<TbItemCat> itemCats = tbItemCatMapper.selectByExample(tbItemCatExample);
        return itemCats;
    }

    public TbItemParam selectItemParamByItemCatId(Long itemCatId) {
        TbItemParamExample example = new TbItemParamExample();
        TbItemParamExample.Criteria criteria = example.createCriteria();
        criteria.andItemCatIdEqualTo(itemCatId);
        List<TbItemParam> list = tbItemParamMapper.selectByExampleWithBLOBs(example);
        if ( list.size()>0){
            return list.get(0);
        }
        return null;
    }

    public Integer insertTbItem(TbItem tbItem, String desc, String itemParams) {
        Date date = new Date();
        //随机产生的Id
        long uuid = IDUtils.genItemId();
        tbItem.setCreated(date);
        tbItem.setUpdated(date);
        tbItem.setStatus((byte)1);
        tbItem.setId(uuid);
        Integer a = tbItemMapper.insert(tbItem);
        //desc_id
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setItemId(uuid);
        Integer b = tbItemDescMapper.insert(tbItemDesc);
        //param_id
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setCreated(date);
        tbItemParamItem.setUpdated(date);
        tbItemParamItem.setItemId(uuid);
        tbItemParamItem.setParamData(itemParams);
        Integer c = tbItemParamItemMapper.insert(tbItemParamItem);
        return a+b+c;
    }

    public Integer deleteItemById(Long itemId) {
        TbItem tbItem = new TbItem();
        tbItem.setId(itemId);
        tbItem.setStatus((byte)4);
       return tbItemMapper.updateByPrimaryKeySelective(tbItem);
    }

    public Integer delete(Long itemId) {
        return tbItemMapper.deleteByPrimaryKey(itemId);
    }

    public Map<String, Object> findById(Long itemId) {
        Map<String,Object> map = new HashMap<>();
        //商品
      TbItem  shop =   tbItemMapper.selectByPrimaryKey(itemId);
      map.put("item",shop);
        //类目
      TbItemCat tbItemCat = tbItemCatMapper.selectByPrimaryKey(shop.getCid());
      map.put("itemCat",tbItemCat.getName());
        //描述
        TbItemDesc tbItemDesc = tbItemDescMapper.selectByPrimaryKey(itemId);
        map.put("itemDate",tbItemDesc.getItemDesc());
        //参数
        return map;
    }

    public Integer update(TbItem tbItem, String desc, String itemParams) {
        Date date = new Date();
        //随机产生的Id
        long uuid = IDUtils.genItemId();
        tbItem.setCreated(date);
        tbItem.setUpdated(date);
        tbItem.setStatus((byte)1);
        tbItem.setId(uuid);
        Integer a = tbItemMapper.updateByPrimaryKeySelective(tbItem);
        //desc_id
        TbItemDesc tbItemDesc = new TbItemDesc();
        tbItemDesc.setCreated(date);
        tbItemDesc.setUpdated(date);
        tbItemDesc.setItemDesc(desc);
        tbItemDesc.setItemId(uuid);
        Integer b = tbItemDescMapper.updateByPrimaryKeySelective(tbItemDesc);
        //param_id
        TbItemParamItem tbItemParamItem = new TbItemParamItem();
        tbItemParamItem.setCreated(date);
        tbItemParamItem.setUpdated(date);
        tbItemParamItem.setItemId(uuid);
        tbItemParamItem.setParamData(itemParams);
        Integer c = tbItemParamItemMapper.updateByPrimaryKeySelective(tbItemParamItem);
        return a+b+c;
    }

    public PageResult selectItemParamAll() {
        PageHelper.startPage(1,5555555);
        List<TbItemParam> list = tbItemParamMapper.selectByExampleWithBLOBs(null);
        PageInfo<TbItemParam> pageInfo = new PageInfo<>(list);
        return new  PageResult(1,pageInfo.getTotal(),pageInfo.getList());
    }


    public Integer insertItemParam(TbItemParam itemParam) {
        Date date = new Date();
        itemParam.setCreated(date);
        itemParam.setUpdated(date);
        return tbItemParamMapper.insert(itemParam);
    }

    public Integer deleteItemParamById(Long id) {
       TbItemParam tbItemParam = new TbItemParam();
        return tbItemParamMapper.deleteByPrimaryKey(id);
    }
}
